package com.serviceprovider.business.module.admin.shipping.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.shipping.model.ShipMode;
import com.serviceprovider.business.module.admin.shipping.services.dao.interfaces.IShipMode;
import com.serviceprovider.business.utility.Int2DoublePropertyEditor;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * @author NehaSiroya
 */
@Controller
public class AdminShipController {
    @Autowired
    private IShipMode shipModeService;

    /**
     * Added by NehaSiroya
     *
     * @param sess
     * @return
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Integer.class, "price", new Int2DoublePropertyEditor(true));
    }

    @RequestMapping(
        value  = RequestConstants.ShippingRequests.SHIP_MODE,
        method = RequestMethod.GET
    )
    public ModelAndView adminShip() {
        ModelAndView   mav          = new ModelAndView(RequestConstants.ShippingRequests.VIEW_SHIP_MODE);
        List<ShipMode> listShipMode = shipModeService.list();
        ShipMode       newShipMode  = new ShipMode();

        mav.addObject("shipModeList", listShipMode);
        mav.addObject("newShipMode", newShipMode);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ShippingRequests.SHIP_MODE,
        method = RequestMethod.POST
    )
    public ModelAndView createNewShipMode(@ModelAttribute("newShipMode") ShipMode form) {
        shipModeService.create(form);

        return new ModelAndView("redirect:" + RequestConstants.ShippingRequests.SHIP_MODE);
    }

    @RequestMapping(
        value  = RequestConstants.ShippingRequests.UPDATE_SHIP_MODE,
        method = RequestMethod.GET
    )
    public ModelAndView updateShipMode(HttpServletRequest request) {
        int      id       = Integer.parseInt(request.getParameter("id"));
        String   mode     = request.getParameter("mode");
        Integer  price    = Integer.parseInt(request.getParameter("price"));
        boolean  active   = Boolean.parseBoolean(request.getParameter("active"));
        ShipMode shipMode = new ShipMode();

        shipMode.setId(id);
        shipMode.setMode(mode);
        shipMode.setPrice(price);
        shipMode.setActive(active);
        shipModeService.modify(shipMode);

        return new ModelAndView("redirect:" + RequestConstants.ShippingRequests.SHIP_MODE);
    }

    @RequestMapping(
        value  = RequestConstants.ShippingRequests.DELETE_SHIP_MODE,
        method = RequestMethod.GET
    )
    public ModelAndView deleteShipMode(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));

        shipModeService.delete(id);

        return new ModelAndView("redirect:" + RequestConstants.ShippingRequests.SHIP_MODE);
    }
}



